package com.sgb.kjwl.viewmodel.self.website;

import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.self.WebsitePhotoEntity;
import com.sgb.kjwl.model.entity.self.WebsitePhotoListEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.websiteManage.WebsitePhotoActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WebsitePhotoModel3 extends BaseViewModel {

    private WebsitePhotoActivity3 activity;
    private SelfRepertory repertory;

    private int page = 1;
    private int pageSize = 20;

    public WebsitePhotoModel3(WebsitePhotoActivity3 activity3, SelfRepertory tr){
        this.activity = activity3;
        this.repertory = tr;
    }

    public void getPhotoData(String id){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().getWebsitePhoto(id,page+"",pageSize+"").enqueue(new Callback<BaseEntity<ProListEntity<List<WebsitePhotoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<WebsitePhotoEntity>>>> call, Response<BaseEntity<ProListEntity<List<WebsitePhotoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getPhotoData(response.body().getData().getList());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<WebsitePhotoEntity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //保存到服务器
    public void savePhotoData(WebsitePhotoListEntity id){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().saveWebsitePhoto(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    EventBus.getDefault().post("delete_website_photo");
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void uploadMutiFileModel(File file, int completeNum,int total) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中"+completeNum+"/"+total+"...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.uploadImgSuccess(response.body().getData().toString());
                    Log.v("uploadFile","success--single");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void uploadMutilFile(List<File> fileList){
        DialogHelper.showProgressDialog(activity, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, RequestBody> photos = new HashMap<>();
        int i = 0;
        for (File file : fileList) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("file" + "\"; filename=\"" + i + "image.jpg\"", requestBody);
            i++;
        }
        //这个接口返回的是一个图片地址,不是List<String> 估计是接口不支持多图上传
        APIManager.getInstance().getUploadAPI().uploadMutiFile(photos).enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    Log.v("uploadFile","success--mutil");
//                    activity.uploadImgSuccess(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
                Log.v("uploadFile",t.getMessage());
            }
        });
    }


}
